<?php
/**
 * 安装队类
 * Enter description here ...
 * @author df
 *
 */
class WorkerAction extends BaseAction{
	
	function wlist(){
		$upage=$this->in("upage");
		if($_SESSION["wlist"]){
			if($upage && $upage!=$_SESSION["wlist"]){
				$_SESSION["wlist"]=$upage;
			}
			$pageSize=$_SESSION["wlist"];
		}else{
			if($upage){
				$_SESSION["wlist"]=$upage;
				$pageSize=$_SESSION["wlist"];
			}else{
				$pageSize=20;
			}
		}
		//每页显示多少个
		//$pageSize=20;
		$m=M();
		if(!$type){
			$type='u';
		}
		$sql="select as_user.U_ID,U_NAME,U_RNAME,U_PHONE,U_EMAIL,U_MOBILE,as_user.IS_BLOCK from as_worker
			inner join as_user on as_worker.U_ID=as_user.U_ID
			where as_user.IS_DEL=0 ";
		if($this->in("U_NAME")){
			$this->assign('U_NAME',$this->in("U_NAME"));
			$sql.=" and U_NAME like '%".$this->in("U_NAME")."%' ";
		}
		if($this->in("U_RNAME")){
			$this->assign('U_RNAME',$this->in("U_RNAME"));
			$sql.=" and U_RNAME like '%".$this->in("U_RNAME")."%' ";
		}
		
		$__COUNT="select COUNT(U_ID) as count FROM (".$sql.") as _user";
		
		$list=$m->query($__COUNT);//使用总数
		/*if($role){
			$list=ExUse::filterDataByRole($list,$role);
		}*/
		if($_GET['p']){
			$p=$_GET['p'];
			$s=($p-1)*$pageSize;
			$sql.=" limit {$s},{$pageSize}";
		}
		else{
			$sql.=" limit 0,{$pageSize}";
		}
		
		$data=$m->query($sql);
		/*
		if($role){
			$data=ExUse::filterDataByRole($data,$role);
		}*/
		$this->assign("userList",$data);
		import('ORG.Util.Page');
		//$count      = count($list);// 查询满足要求的总记录数
		$count      = $list[0]["count"];// 查询满足要求的总记录数
		$Page       = new Page($count,$pageSize);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
	}
	/**
	 * 添加安装队
	 * Enter description here ...
	 */
	function add(){
		if($this->in("op")){
			$data=$this->in("data");
			$uaccount=$data["U_NAME"];
			//检测用户是否重复
			$role=D("User");
			$search=array();
			$search['U_NAME']=$uaccount;
			$ds=$role->where($search)->count();
			$out=array();
			if($ds){
				$out=array("info"=>"会员名已存在","status"=>"n");
				
			}else{
				$user=D("User");
				$data["U_PWD"]=md5($data["U_PWD"]);
				//$data["u_last_logintime"]=time();
				$data["U_LAST_LOGIP"]=get_client_ip();
				$data["CREATED_AT"]=time();
				$data["UPDATED_AT"]=time();
				$data["U_CREATER"]=ExSession::getSession()->getUserId();
				$data["U_GU_ID"]=getGuId();
				$rev=$user->add($data);
				if($rev){
					//添加角色
					$role=6;//安装队
					$rl=D("User_role");
					$d=array();
					$d["U_ID"]=$rev;
					$d["R_ID"]=$role;
					$rl->add($d);
					
					//写入安装队表
					$worker=D("Worker");
					$d=array();
					$d["U_ID"]=$rev;
					$d["CS_REG_TIME"]=time();
					$d["CREATED_AT"]=time();
					$d["UPDATED_AT"]=time();
					$worker->add($d);
					
					//增加服务队与安装人员的关系
					$ta=D("Srv_team2worker");
					$d=array();
					$d["ST_ID"]=$data["ST_ID"];
					$d["U_ID"]=$rev;
					$ta->add($d);
					$out=array("info"=>"安装人员添加成功","status"=>"y");
				}else{
					$out=array("info"=>"安装人员添加失败","status"=>"n");
				}
			}
			echo json_encode($out);
			exit;
		}
		$this->assign("edit",'n');//说明不是编辑
		//指定要提交的action
		$this->assign("action","?g=Admin&m=Worker&a=add");
		$this->display();
	}
	/**
	 * 编辑安装人员
	 * Enter description here ...
	 */
	function edit(){
		
		$data=$this->in("data");
		$u_id=$this->in("uid");
		/*if(!$u_id){
			$u_id=ExSession::getSession()->getUserId();
		}
		*/
		if($this->in("op") && $data){
			$rev1="n";
			$info='安装人员编辑失败';
			$user=D("User");
			$user->find($data['U_ID']);
			$user->U_RNAME=$data["U_RNAME"];
			//判断手机号是否改变 如果改变判断是否重复
			if($user->U_MOBILE!=$data["U_MOBILE"]){
				$user1=D("User");
				
				$_s=array();
		   		$_s["U_MOBILE"]=$data["U_MOBILE"];
		   		$uData=$user1->where($_s)->count('U_ID');
		   		if($uData){
		   			$rev1='n';
					$info='手机号码重复';
					$arr=array("info"=>$info,"status"=>$rev1);
					echo json_encode($arr);
					exit;
		   		}
			}
			
			//判断邮箱是否改变 如果改变判断是否重复
			if($user->U_EMAIL!=$data["U_EMAIL"]){
				$user2=D("User");
				
				$_s=array();
		   		$_s["U_EMAIL"]=$data["U_EMAIL"];
		   		$uData=$user2->where($_s)->count('U_ID');
		   		if($uData){
		   			$rev1='n';
					$info='邮箱重复';
					$arr=array("info"=>$info,"status"=>$rev1);
					echo json_encode($arr);
					exit;
		   		}
			}
			
			$user->U_MOBILE=$data["U_MOBILE"];
			$user->U_EMAIL=$data["U_EMAIL"];
			$user->U_PHONE=$data["U_PHONE"];
			$user->UPDATED_AT=time();
			$rev=$user->save();
			if($rev){
				$ta=D("Srv_team2worker");
				$s=array();
				$s["U_ID"]=$u_id;
				//先删除再添加
				$ta->where($s)->delete();
				$ta=D("Srv_team2worker");
				$d=array();
				$d["ST_ID"]=$data["ST_ID"];
				$d["U_ID"]=$u_id;
				$ta->add($d);
				$info='安装人员编辑成功';
			}
			$arr=array("info"=>$info,"status"=>$rev1);
			echo json_encode($arr);
			exit;
		}
		if($u_id){
			$user=D("User");
			$uData=$user->where('U_ID='.$u_id)->select();
			
			$this->assign('uData',$uData[0]);
			
			//找用户的服务队
			$ta=D("Srv_team2worker");
			$s=array();
			$s["U_ID"]=$u_id;
			$sts=$ta->where($s)->field("ST_ID")->select();
			$sname='';
			$sid='';
			if($sts && is_array($sts)){
				foreach ($sts as $val){
					if($sid==''){
						$sid=$val["ST_ID"];
						$sname=$uData[0]["U_RNAME"];
					}else{
						$sid=$sid.",".$val["ST_ID"];
						$sname=$sname.",".$uData[0]["U_RNAME"];
					}
				}
			}
			$this->assign("sname",$sname);
			$this->assign("sid",$sid);
			
		}
		$this->assign("uid",$u_id);
		$this->assign("edit",'y');//说明是编辑
		//指定要提交的action
		$this->assign("action","?g=Admin&m=Worker&a=edit");
		$this->display();
	}
	
	/**
	 * 删除用户
	 * Enter description here ...
	 */
	function del(){
		$u_id=$this->in("uid");
		
		$u_list=explode(',', $u_id);
		if(is_array($u_list) && count($u_list)){
			foreach ($u_list as $key=>$uid){
				$user=D("User");
				$user->find($uid);
				$user->IS_DEL=1;
				$user->UPDATED_AT=time();
				$rev=$user->save();
				if($rev){
					$worker=D("Worker");
					$worker->find($uid);
					$worker->IS_DEL=1;
					$worker->UPDATED_AT=time();
					$rev=$worker->save();
				}
			}
		}
		
		if($rev){
			echo "y";
		}else{
			echo "n";
		}
		exit;
	}
}